export default class Cookie {
	static getCookie(key) {
		if (key == null) {
			return document.cookie ? document.cookie.split('; ').map(item => ({
				[item.split('=')[0]]: item.split('=')[1]
			})) : null;
		}
		let resultArr = new RegExp(`(?:^| )${key}=([^;]*)(?:; |$)`, 'i').exec(document.cookie);
		return resultArr ? decodeURIComponent(resultArr[1]) : null;
	}
	static setCookie(key, val, opt = {}) {
		if (opt.expireDays) {
			let currDate = new Date();
			currDate.setDate(currDate.getDate() + opt.expireDays);
			opt.expires = currDate.toUTCString();
		}
		return document.cookie = [[key, encodeURIComponent(val)]].concat(Object.entries(opt)).map(item => item.join('=')).join('; ');
	}
	static removeCookie(key) {
		if (key == null) {
			return false;
		}
		return document.cookie = Cookie.setCookie(key, '', {
			'expires': -1
		});
	}
	static clearCookie() {
		Object.keys(Cookie.getCookie() || {}).forEach(item => Cookie.removeCookie(item));
		return !document.cookie;
	}
}
